﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using CustomsSystem.BLL;
using Maticsoft.Common;
using CustomsSystem.Model;

namespace WEB.workflow
{
    public partial class workflowAppointo : System.Web.UI.Page
    {
        public static DataSet ds;
        public WorkflowHandleManage bll;
        protected void Page_Load(object sender, EventArgs e)
        {
            int id = Convert.ToInt32(Request.QueryString["ID"]);
            ds = new DataSet();
            bll = new WorkflowHandleManage();
            //ds = bll.GetList("sheetID=" + id.ToString());
            ds = bll.GetView("sheetID=" + id.ToString());
            gvWorkflowList.DataSource = ds;

            if (!IsPostBack)
            {
                if (Request.QueryString["ID"] != null)   //修改
                {
                    gvWorkflowList.DataBind();

                    int rowsCount = ds.Tables[0].Rows.Count;
                    if (rowsCount < 1)
                    {
                        MessageBox.ShowAndRedirect(this, "该委托单尚未启动工作流", Request.UrlReferrer.ToString());
                        return;
                    }
                    tbCurBus.Text = ds.Tables[0].Rows[rowsCount - 1]["busName"].ToString();
                    string strRec = ds.Tables[0].Rows[rowsCount - 1]["receiveMark"].ToString();
                    if (strRec == "0")
                    {
                        tbIsReceive.Text = "未接收";
                    }
                    else
                    {
                        tbIsReceive.Text = "已接收";
                    }
                }
                else
                {
                    MessageBox.Show(this, "访问已过期，请后退到上一页");
                }
                
                //判断权限，并且隐藏或者显示指派页面
                string curJobNo = Session["JobNo"].ToString();
                if (curJobNo == "00")
                    BindEmpData(ddlEmp);
                else
                    BindEmpData(ddlEmp, curJobNo);
                if (curJobNo == "00" || curJobNo == "01" || curJobNo == "0101" || curJobNo == "0102")
                {
                    //Panel1.Visible = true;
                    Panel2.Visible = true;
                }
                else
                {
                    //Panel1.Visible = false;
                    Panel2.Visible = false;
                }
                
                //添加工作流步骤
                for (int i = 1; i <= gvWorkflowList.Rows.Count; i++)
                {
                    string text=gvWorkflowList.Rows[i-1].Cells[0].Text;
                    string value=gvWorkflowList.DataKeys[i-1].Value.ToString();
                    ddlStep.Items.Add(new ListItem(text, value));
                }
            }
        }

        /// <summary>
        /// 图片的判断显示
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void gvWorkflowList_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                WorkflowHandleManage bll = new WorkflowHandleManage();
                //CustomsSystem.Model.workflow model;
                int sheetID = Convert.ToInt32(Request.QueryString["ID"]);
                int IntimeFlag;
                CustomsSystem.Model.workflow model;
                int wfID = Convert.ToInt32(gvWorkflowList.DataKeys[e.Row.RowIndex].Value);
                model = bll.GetModel(wfID);
                //先判断报关还是报检
                WorkflowManage wfMan = new WorkflowManage();
                int type = 0;
                if (model.infoWKID != null) type = model.infoWKID.Value;
                Info_workflowMaster modelMaster = wfMan.GetModelMaster(type);
                if (modelMaster != null)
                {
                    if (modelMaster.sheetTypeID == 1) type = 1;
                    else if (modelMaster.sheetTypeID == 2) type = 2;
                }
                //=============================
                if ((model.doEmpNo == "" || model.doEmpNo == null) && model.busID != 99)//如果是当前环节或者完结
                {
                    IntimeFlag = bll.GetIntime(sheetID,type);//判断及时度
                }
                else  //读取就好
                {
                    if (model.ontimeType != null) IntimeFlag = (int)model.ontimeType;
                    else IntimeFlag = 0;
                }

                Image tmp_ImageIntime = (Image)e.Row.Cells[6].FindControl("ImgIntime");//获得图片列的控件
                if (tmp_ImageIntime != null)
                {
                    string imagepath = "";
                    if (IntimeFlag == 1)
                    {
                        imagepath = "../images/status/Green.png";
                    }
                    else if (IntimeFlag == 2)
                    {
                        imagepath = "../images/status/Yellow.png";
                    }
                    else
                    {
                        imagepath = "../images/status/Red.png";
                    }
                    tmp_ImageIntime.ImageUrl = imagepath;//显示图片
                }
            }
        }

        /// <summary>
        /// 分页绑定
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void gvWorkflowList_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gvWorkflowList.PageIndex = e.NewPageIndex;
            //gvCustomerManage.DataSource = dt;
            gvWorkflowList.DataBind();//重新绑定GridView数据的函数
        }
        
        /// <summary>
        /// 指派一个角色处理
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnAppoint_Click(object sender, EventArgs e)
        {
            int id = Convert.ToInt32(gvWorkflowList.DataKeys[gvWorkflowList.Rows.Count-1].Value);
            CustomsSystem.Model.workflow model = new CustomsSystem.Model.workflow();
            WorkflowHandleManage bll = new WorkflowHandleManage();
            model = bll.GetModel(id);
            model.recDate = null;
            model.jobEmpNo = null;
            model.doEmpNo = null;
            model.receiveMark = 0;

            model.jobNO = ddlEmp.SelectedValue;

            if (bll.Update(model))
            {
                MessageBox.ShowAndRedirect(this, "指派成功,请及时通知该员工！",Request.Url.ToString());
            }
        }

        //修改工作流及时度
        protected void btnEditIntime_Click(object sender, EventArgs e)
        {
            int id = Convert.ToInt32(ddlStep.SelectedValue);
            CustomsSystem.Model.workflow model = new CustomsSystem.Model.workflow();
            WorkflowHandleManage bll = new WorkflowHandleManage();
            model = bll.GetModel(id);
            model.ontimeType = Convert.ToInt32(ddlIntime.SelectedValue);
            if (bll.Update(model))
            {
                MessageBox.ShowAndRedirect(this, "修改成功！", Request.Url.ToString());
            }
        }

        /// <summary>
        /// 绑定员工数据
        /// </summary>
        /// <param name="ddl"></param>
        private void BindEmpData(DropDownList ddl)
        {
            JobManage jobBll = new JobManage();
            List<Info_job> jobModels = jobBll.GetModelList("");
            foreach (Info_job aJob in jobModels)
            {

                ddl.Items.Add(new ListItem(aJob.jobName, aJob.jobNo));
            }
        }

        private void BindEmpData(DropDownList ddl, string jobNO)
        {
            JobManage jobBll = new JobManage();
            List<Info_job> jobModels = jobBll.GetModelList("");
            foreach (Info_job aJob in jobModels)
            {
                int len = jobNO.Length;
                if(aJob.jobNo.Length >=len && aJob.jobNo.Substring(0,len) == jobNO)
                    ddl.Items.Add(new ListItem(aJob.jobName, aJob.jobNo));
            }
        }
    }
}